*****************************************************************************
*SET UP
*****************************************************************************
global root_dir = ""
di "$root_dir"

do "${root_dir}/Code/setup.do"

****************************************************************************************
*SUMMARY STATS
****************************************************************************************
use "$workdir/regfile_agent_month" if agent_active==1, clear

gen control=(ever_treated==0)
gen treated_pre=(ever_treated==1 & post_treat==0)
gen treated_post=(ever_treated==1 & post_treat==1)
gen all=1

gen flag_us=(agent_modecountry=="US")

foreach var of varlist all control treated_pre treated_post  {

qui: distinct agent_id if `var'==1
local Agents_`var'=r(ndistinct)

qui: distinct agent_modeteam if `var'==1
local Teams_`var'=r(ndistinct)

qui: summ flag_us if `var'==1
local US_`var'=r(mean)

qui: distinct agent_modeloc if `var'==1
local Loc_`var'=r(ndistinct)

qui: summ nchats if `var'==1 
local AvgChats_`var'=r(mean)

qui: summ aht if `var'==1 
local NDur_`var'=r(mean)

qui: summ ahtsd if `var'==1 
local stdDur_`var'=r(mean)

qui: summ fcr_cresta if `var'==1 
local fcr_`var'=r(mean)

qui: summ rph_cresta if `var'==1 
local rph_`var'=r(mean)

qui: summ tnps_cresta [aweight=survey_count] if `var'==1 
local tnps_`var'=r(mean)


global Chats_`var': di %12.0gc `Chats_`var''
global Agents_`var': di %12.0gc `Agents_`var''
global Teams_`var': di %12.0gc `Teams_`var''
global US_`var': di %12.2gc `US_`var''
global Loc_`var': di %12.2gc `Loc_`var''
global AvgChats_`var' : di %12.2gc `AvgChats_`var''
global NDur_`var' : di %12.2gc `NDur_`var''
global stdDur_`var' : di %12.2gc `stdDur_`var''
global tnps_`var' : di %12.2gc `tnps_`var''
global fcr_`var' : di %12.2gc `fcr_`var''
global rph_`var' : di %12.2gc `rph_`var''
}

******************************************************************************************
* TABLE I - Summary Statistics for the Sample of Customer Service Agents
******************************************************************************************


texdoc init "${tables_dir}/tableI.tex", replace force
tex \begin{tabular}{lcccc} \toprule \toprule
tex Variable & All & Never Treated & Treated, Pre & Treated, Post \\ 
tex \addlinespace \hline \addlinespace
tex  
tex \hspace{3mm} Chats   & $Chats_all & $Chats_control  & $Chats_treated_pre & $Chats_treated_post  \\ 
tex \hspace{3mm} Agents   & $Agents_all & $Agents_control  & $Agents_treated_pre& $Agents_treated_post   \\ 
tex \hspace{3mm} Number of Teams   & $Teams_all & $Teams_control  & $Teams_treated_pre & $Teams_treated_post  \\ 
tex \hspace{3mm} Share US Agents  & $US_all & $US_control  & $US_treated_pre  & $US_treated_post  \\ 
tex \hspace{3mm} Distinct Locations   & $Loc_all & $Loc_control  & $Loc_treated_pre & $Loc_treated_post  \\
tex \hspace{3mm} Average Chats per Month   & $AvgChats_all & $AvgChats_control  & $AvgChats_treated_pre & $AvgChats_treated_post  \\ 
*tex \hspace{3mm} Share Outsourced   & $ShareOut_all & $ShareOut_control  & $ShareOut_treated_pre   \\ 
*tex \hspace{3mm} Number of Skills   & $NSkill_all & $NSkill_control  & $NSkill_treated_pre  \\ 
*tex \hspace{3mm} Team Size   & $teamsize_all & $teamsize_control  & $teamsize_treated_pre   \\ 
*tex \hspace{3mm} Technical Documents per Week   & $NKB_all & $NKB_control  & $NKB_treated_pre   \\ 
tex \hspace{3mm} Average Handle Time (Min)   & $NDur_all & $NDur_control  & $NDur_treated_pre  & $NDur_treated_post \\
tex \hspace{3mm} St. Average Handle Time (Min)   & $stdDur_all & $stdDur_control  & $stdDur_treated_pre  & $stdDur_treated_post \\ 
tex \hspace{3mm} Resolution Rate   & $fcr_all & $fcr_control  & $fcr_treated_pre & $fcr_treated_post   \\ 
tex \hspace{3mm} Resolutions Per Hour  & $rph_all & $rph_control  & $rph_treated_pre & $rph_treated_post   \\ 
tex \hspace{3mm} Customer Satisfaction (NPS)  & $tnps_all & $tnps_control  & $tnps_treated_pre   & $tnps_treated_post \\ 
tex \addlinespace \bottomrule \bottomrule
tex \addlinespace \bottomrule \bottomrule
tex \end{tabular}
texdoc close


****************************************************************************
*FIGURE I: Raw Productivity Distributions, by AI Treatment
****************************************************************************

use "$workdir/regfile_agent_month" if agent_active==1, clear

assert pre_treat==1 | post_treat==1 | ever_treated==0

foreach y in aht cph fcr_cresta rph_cresta {

	if "`y'" == "aht" {
	local ytext= "Average Handle Time" 
	local fname = "figureI_panelB"
	}
	else if "`y'" == "cph" {
	local ytext= "Chats Per Hour" 
	local fname = "figureI_panelC"
	}
	else if "`y'" == "fcr_cresta" {
	local ytext ="Share Resolved"
	local fname = "figureI_panelD"
	}
	else if "`y'" == "rph_cresta" {
	local ytext ="Resolutions Per Hour" 
	local fname = "figureI_panelA"
	}

summ `y', detail
twoway (kdensity `y' if pre_treat==1 & `y' < `r(p99)',  color(red%40) recast(area) lpattern(shortdash)) /// 
(kdensity `y' if post_treat==1 & `y' < `r(p99)',  color(blue%40) recast(area) lpattern(longdash)) /// 
(kdensity `y' if ever_treated==0 & `y' < `r(p99)',  color(green%40) recast(area) lpattern(solid)), /// 
legend(order(1 "Pre AI" 2 "Post AI" 3 "Never AI") rows(3) ring(0) pos(9) size(*$legendx))  $graphbackground xtitle("`ytext'", size($xtitlesize)) ytitle("Density", size($ytitlesize)) xlabel(, labsize($xlabelsize) nogrid) ylabel(, labsize($ylabelsize) nogrid)
graph export "${figures_dir}/`fname'.eps", as(eps) preview(on) replace  fontface($graphfont)
}


use "$dlwork/regfile_agent_month" if agent_active==1, clear

assert pre_treat==1 | post_treat==1 | ever_treated==0

foreach y in tnps_cresta {
    if "`y'" == "tnps_cresta" {
	local ytext ="Customer Satisfaction (NPS)" 
	local fname="figureI_panelE"
	}

summ `y', detail
twoway (kdensity `y' if pre_treat==1 & `y' < `r(p99)' [aweight=survey_count],  color(red%40) recast(area)) ///
 (kdensity `y' if post_treat==1 & `y' < `r(p99)' [aweight=survey_count],  color(blue%40) recast(area) ) (kdensity `y' if ever_treated==0 & `y' < `r(p99)'  [aweight=survey_count],  color(green%40) recast(area) ), legend(order(1 "Pre AI" 2 "Post AI" 3 "Never AI") pos(9) rows(3) ring(0) size(*$legendx))  $graphbackground xtitle("`ytext'", size($xtitlesize)) ytitle("Density", size($ytitlesize)) xlabel(, labsize($xlabelsize) nogrid) ylabel(, labsize($ylabelsize) nogrid)
graph export "${figures_dir}/`fname'.eps", as(eps) preview(on) replace  fontface($graphfont)
}
